﻿using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Write.API.Command.LeaveCommand;
using MediatR;

namespace B.S.BaseData.Write.API.CommandHandler.LeaveHandler
{
    public class DelLeaveHandler : IRequestHandler<DelLeaveCommand, APIResult<int>>
    {
        private readonly IBaseRepository<Leave> leaveRep;

        public DelLeaveHandler(IBaseRepository<Leave> leaveRep)
        {
            this.leaveRep = leaveRep;
        }
        public Task<APIResult<int>> Handle(DelLeaveCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var model = leaveRep.GetAll().Where(x=>x.Id==request.Id).FirstOrDefault();
            if (model != null) 
            {
                model.IsDeleted = true;
                leaveRep.Update(model);
                result.Code = APIEnum.Success;
                result.Msg = "删除成功";
                result.Data = 1;
            }
            else
            {
                result.Code=APIEnum.Failed;
                result.Msg = "删除失败";
                result.Data = -1;
            }
         
            return Task.FromResult(result);
            
        }
    }
}
